<%@page import="com.myx.pojo.Comment"%>
<%@ page language="java" import="com.myx.pojo.News" pageEncoding="UTF-8"%>
<%@ page import="com.myx.service.comment.CommentService" %>
<%@ page import="com.myx.service.comment.CommentServiceImpl" %>
<%@ page import="com.myx.service.user.UserService" %>
<%@ page import="com.myx.service.user.UserServiceImpl" %>
<%@ page import="java.util.List" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/common.css">
  </head>
<body>
<!--页面的头部-->
<%@ include file="common/detailHeader.jsp"%>
<!--页面的主体-->
<div id="content" class="main-content clearfix">
	<!--主体的的左边部分-->
    <%@ include file="common/leftSideBar.jsp"%>
    <!--页面主体的右部，包括新闻的列表和评论内容 -->
    <%
        String newsId=request.getParameter("newsId");
        NewsService newsService=new NewsServiceImpl();
        News news=newsService.getNewsById(Integer.parseInt(newsId));
    %>
    <%
        UserService userService=new UserServiceImpl();
        User userById= userService.getUserById(news.getCreateBy());
    %>
    <!--页面主体的右部，包括新闻的列表和评论内容 -->
    <div class="main-content-right">
        <!--各专题的新闻列表-->
        <div class="main-text-box">
            <div class="article-place"><a href="#">新闻中心</a> > <a href="#">国内</a></div>
      		<div class="main-text-box-tbg">
                <div class="main-text-box-bbg">
                    <div class="article-box">
                    	<!--新闻的标题-->
						<h1><%=news.getTitle()%></h1>
                        <div class="source-bar">

						发布者：<%=userById.getUsername()%> 分类：新闻信息 发布时间：
                         <%=news.getCreateDate() %>
                        </div>
                        <div class="article-content">
                            <span class="article-summary"><b>摘要：</b><%=news.getSummary()%></span>
                            <p><%=news.getContent()%> </P>
                        </div>
                        <div class="comment">
                            <%
                                CommentService commentService= new CommentServiceImpl();
                                List<Comment> commentList= commentService.getCommentListByNewsId(news.getId());

                            %>
<%--                            通过循环遍历得到评论创建人以及创建时间--%>
                            <dl>
                                <%
                                    for (int i=0;i<commentList.size();i++){
                                %>
                                <dt class="comment-top">
                                    <span class="fr"><%=commentList.get(i).getCreateDate()%></span>
                                    <b><%=(i+1)%>楼</b> <b><%=commentList.get(i).getCreateBy()%></b>
                                </dt>
                                <dd class="comment-body">
                                    <%=commentList.get(i).getContent()%>
                                </dd>
                                <%
                                    }
                                %>
                            </dl>
                        </div>
                        <div class="comment-form">
                            <div class="comment-form-header">
                                <%
                                    String username2 = null;
                                    if (user!=null) {
                                        username2 = user.getUsername();
                                    }
                                    if (username2!=null) {
                                %>
                                <span>用户：<%=username2%></span>
                                <%
                                    }else{
                                %>
                                <span>用户：请登录</span>
                                <%
                                    }
                                %>
                                <h3>发表评论：</h3>
                            </div>
                            <div class="comment-form-content">
                                <%
                                    if (user != null) {
                                %>
                                <input id="userId" type="hidden" value="<%=user.getId() %>">
                                <%
                                    }
                                %>
                                <input id="newsId" type="hidden" value="<%=news.getId()%>">
                                <textarea class="comment-textarea" id="textArea"></textarea>
                                <button type="submit" class="comment-btn" id="commentBtn">评论</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--//-->
        
    </div>
</div>
<!--页面底部-->
<%@ include file="common/footer.jsp"%>
<script src="<%=request.getContextPath()%>/js/jquery-3.6.0.js"></script>
<script src="<%=request.getContextPath()%>/js/addComment.js"></script>
</body>
</html>